package com.extension.company.common.widget.popuwindow;

import android.app.Activity;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.extension.company.admin.R;


public class PwWithdrawCash extends PopupWindow implements View.OnClickListener {

    private Activity context;

    private TextView tv_cancel, tv_ok;

    private OnPwWithdrawCashListener mOnPwWithdrawCashListener;

    public PwWithdrawCash(Activity context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View mMenuView = inflater.inflate(R.layout.pw_withdrawcash, null);

        initView(mMenuView);

        initData();

        setListener();

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点�?
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背�?
        this.setBackgroundDrawable(dw);
        //设置动画
        setAnimationStyle(R.style.popwin_anim_style);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在�?�择框外面则�?毁弹出框
        //点击外边框关�?
        setOutsideTouchable(true);
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int top = mMenuView.findViewById(R.id.layout_main).getTop();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    int y = (int) event.getY();
                    if (y < top) {
                        PwWithdrawCash.this.dismiss();
                    }
                }
                return true;
            }
        });
    }

    //初始化view
    private void initView(View mMenuView) {
        tv_cancel = mMenuView.findViewById(R.id.tv_cancel);
        tv_ok = mMenuView.findViewById(R.id.tv_ok);
    }

    private void initData() {

    }

    private void setListener() {
        tv_cancel.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
        this.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                if (mOnPwWithdrawCashListener != null) {
                    mOnPwWithdrawCashListener.cancel();
                }
                dismiss();
                break;
            case R.id.tv_ok:
                if (mOnPwWithdrawCashListener != null) {
                    mOnPwWithdrawCashListener.ok();
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    public interface OnPwWithdrawCashListener {
        void cancel();
        void ok();
    }

    public void setOnPwAddressListAddListener(OnPwWithdrawCashListener mOnPwWithdrawCashListener) {
        this.mOnPwWithdrawCashListener = mOnPwWithdrawCashListener;
    }

    public void setTitle(String title){

    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 20);
        } else {
            this.dismiss();
        }
    }
    /**
     * 显示
     */
    public void show() {
        if (!this.isShowing()) {
            hideSoftInputView();
            this.showAtLocation(((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0), Gravity.CENTER, 0, 0);
            backgroundAlpha(0.7f);
        } else {
            this.dismiss();
        }
    }

    private void backgroundAlpha(float bgAlpha) {
        if (bgAlpha == 1) {
            //不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
            context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            //此行代码主要是解决在华为手机上半透明效果无效的bug
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0~1.0
        context.getWindow().setAttributes(lp); //act 是上下文context
    }

    //强制隐藏软盘
    public void hideSoftInputView() {
        InputMethodManager manager = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));
        if (context.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (context.getCurrentFocus() != null)
                manager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
